package org.fage.semples.dao;

import java.util.ArrayList;
import java.util.List;

import org.fage.semples.model.ExamTest;
import org.fage.semples.model.ExamTestTypeVO;
import org.fage.semples.model.ExamTestVO;
import org.fage.semples.model.PagerModel;
import org.fage.semples.util.SystemContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class ExamTestHiberanteDAOImpI extends BaseHibernateDAO<ExamTest> implements ExamTestHiberanteDAO {

	//多表查询所有
	@Override
	public List<ExamTest> findAllExamTestOrExamTestType(String subId){
		return this.queryForList("FROM ExamTest WHERE sub_id=?", new Object[]{subId});
	}
	
	
	//分页查询
	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<ExamTestVO> findAllByPage() {
		
		PagerModel<ExamTestVO> page = new PagerModel<ExamTestVO>();
		ArrayList<ExamTestVO> dates = new ArrayList<ExamTestVO> ();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getSize();
		//使用hibernate完成分页
		List<ExamTest> tempDates = this.getSession().createQuery(" from ExamTest ").setFirstResult(offset).setMaxResults(pageSize).list();
		for(ExamTest examTest : tempDates){
			ExamTestVO examTestVO = new ExamTestVO();
			BeanUtils.copyProperties(examTest, examTestVO);
			examTestVO.setSubName(examTest.getExamSubject().getName());
			ExamTestTypeVO testTypeVO = new ExamTestTypeVO();
			BeanUtils.copyProperties(examTest.getExamTestType(), testTypeVO);
			examTestVO.setExamTestType(testTypeVO);
			dates.add(examTestVO);
			
		}
		page.setDates(dates);
		page.setOffset(offset);
		page.setPageSize(pageSize);
		
		//获取总记录数
		long totalSize = (long)this.getSession().createQuery("select count(*) from ExamTest").uniqueResult();
		page.setTotalSize(totalSize);
		
		return page;
	}
	
	//单表查询所有
	@Override
	public List<ExamTest> findAllExamTests(){
		return this.find("FROM ExamTest");
	}
	//查询单条记录
	@Override
	public ExamTest getExamTestById(Integer id){
		return (ExamTest) this.get(id);
	}
	//修改
	@Override
	public void editExamTest (ExamTest examTest) throws Exception{
		ExamTest exsitsExamTest = this.get(examTest.getId());
		if(exsitsExamTest != null){
			exsitsExamTest.setId(examTest.getId());
			exsitsExamTest.setChooseA(examTest.getChooseA());
			exsitsExamTest.setChooseB(examTest.getChooseB());
			exsitsExamTest.setChooseC(examTest.getChooseC());
			exsitsExamTest.setChooseD(examTest.getChooseD());
			exsitsExamTest.setContent(examTest.getContent());
			exsitsExamTest.setExamSubject(examTest.getExamSubject());
			exsitsExamTest.setExamTestType(examTest.getExamTestType());
			exsitsExamTest.setAnswer(examTest.getAnswer());
			this.update(exsitsExamTest);
		}
	}
	//添加
	@Override
	public void createExamTest(ExamTest examTest) throws Exception{
		this.save(examTest);
	}
	//删除   
	@Override
	public void deleteExamTest(Integer id) throws Exception{
		ExamTest exsitsExamTest = this.get(id);
		if(exsitsExamTest != null){
			this.delete(exsitsExamTest);
		}
	}
	
}
